package com.xyz.service;

import com.free.common.vo.ResultModel;
import com.free.ext.mybatis.model.entity.PageResponseModel;
import com.xyz.dao.logic.CCustomerfundLogic;
import com.xyz.dao.logic.CCustomerfundflowLogic;
import com.xyz.domain.ext.EnumFundFlowForm;
import com.xyz.domain.ext.EnumMetrialType;
import com.xyz.domain.table.CCustomerfund;
import com.xyz.domain.table.CCustomerfundflow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

@Service
public class CustomerFundService {

    @Autowired
    CCustomerfundLogic customerfundLogic;

    @Autowired
    CCustomerfundflowLogic customerfundflowLogic;

    /**
     * 资金流水列表
     * @param cCustomerfundflow
     * @param pageNo
     * @param pageSize
     * @return
     */
    public PageResponseModel<CCustomerfundflow> customerFundList(CCustomerfundflow cCustomerfundflow, Integer pageNo, Integer pageSize){
        return null;
    }

    public List<CCustomerfundflow> getRangeTypeInfo(CCustomerfundflow cCustomerfundflow){
        return customerfundflowLogic.selectFundFlows(cCustomerfundflow);
    }

    @Transactional
    public ResultModel addFundInfo(EnumFundFlowForm form, Integer custId, BigDecimal beanAmt, BigDecimal iconAmt, BigDecimal diamondAmt){

        boolean isRefresh = false;

        CCustomerfundflow cCustomerfundflow = new CCustomerfundflow();
        cCustomerfundflow.setCustomerId(custId);
        cCustomerfundflow.setForm(form.getKey());
        cCustomerfundflow.setRemark(form.getName());

        beanAmt = ifNullDefault(beanAmt);
        iconAmt = ifNullDefault(iconAmt);
        diamondAmt = ifNullDefault(diamondAmt);

        if(beanAmt != null && beanAmt.compareTo(BigDecimal.ZERO) > 0){
            isRefresh = true;
            cCustomerfundflow.setAmount(beanAmt);
            cCustomerfundflow.setMetrialType(EnumMetrialType.Bean.getKey());
            int res = customerfundflowLogic.insertFundFlowBean(cCustomerfundflow);
            if(res <= 0){
                throw new RuntimeException("");
            }
        }

        if(iconAmt != null && iconAmt.compareTo(BigDecimal.ZERO) > 0){
            isRefresh = true;
            cCustomerfundflow.setAmount(iconAmt);
            cCustomerfundflow.setMetrialType(EnumMetrialType.Icon.getKey());
            int res = customerfundflowLogic.insertFundFlowBean(cCustomerfundflow);
            if(res <= 0){
                throw new RuntimeException("");
            }
        }

        if(diamondAmt != null && diamondAmt.compareTo(BigDecimal.ZERO) > 0){
            isRefresh = true;
            cCustomerfundflow.setAmount(diamondAmt);
            cCustomerfundflow.setMetrialType(EnumMetrialType.Diamond.getKey());
            int res = customerfundflowLogic.insertFundFlowBean(cCustomerfundflow);
            if(res <= 0){
                throw new RuntimeException("");
            }
        }
        if(isRefresh){
            CCustomerfund fund = new CCustomerfund();
            fund.setBeanCnt(beanAmt);
            fund.setIconCnt(iconAmt);
            fund.setDiamondCnt(diamondAmt);
            fund.setUserId(custId);
            int res = customerfundLogic.udpCustomerFund(fund);
            if(res <= 0){
                throw new RuntimeException("");
            }
        }
        return ResultModel.success();
    }

    private BigDecimal ifNullDefault(BigDecimal amt){
        if (amt == null){
            return BigDecimal.ZERO;
        }
        return amt;
    }
}
